import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report
from joblib import dump

class ClassificationModel(object):
    def __init__(self):
        self.df = pd.read_csv('fina_indicator.csv')

    def get_conditions(self):
        """
        分类前准备
        """
        df = self.df
        df['max_ratio'] = df['max_closes'] / df['the_closes']  # 收益潜力
        df['min_ratio'] = df['min_closes'] / df['the_closes']  # 风险程度
        # 划分高低阈值
        high_return_threshold = df['max_ratio'].quantile(0.4)  # 前40%定义为高收益
        high_risk_threshold = df['min_ratio'].quantile(0.4)  # 前40%定义为高风险

        # 生成分类标签
        conditions = [
            (df['max_ratio'] >= high_return_threshold) & (df['min_ratio'] >= high_risk_threshold),
            (df['max_ratio'] < high_return_threshold) & (df['min_ratio'] >= high_risk_threshold),
            (df['max_ratio'] >= high_return_threshold) & (df['min_ratio'] < high_risk_threshold),
            (df['max_ratio'] < high_return_threshold) & (df['min_ratio'] < high_risk_threshold)
        ]
        labels = ['高收益高风险', '低收益高风险', '高收益低风险', '低收益低风险']
        df['category'] = np.select(conditions, labels, default='未知')

        # 标签编码
        label_encoder = LabelEncoder()
        df['category_encoded'] = label_encoder.fit_transform(df['category'])

        # 数据标准化
        scaler = StandardScaler()
        features = df[['eps', 'total_revenue_ps', 'undist_profit_ps', 'gross_margin', 'fcff', 'fcfe',
                    'tangible_asset', 'bps', 'grossprofit_margin', 'npta']]
        X = scaler.fit_transform(features)
        y = df['category_encoded']
        # 划分训练集和测试集
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=24)
        dump(scaler, 'scaler.joblib')
        dump(label_encoder, 'label_encoder.joblib')
        return X_train, X_test, y_train, y_test, label_encoder

    def knn_utils(self, X_train, X_test, y_train, y_test, label_encoder):
        """
        KNN 分类
        """
        # 初始化 KNN 分类器
        knn = KNeighborsClassifier(n_neighbors=5)
        # 训练模型
        knn.fit(X_train, y_train)
        # 预测测试
        y_pred = knn.predict(X_test)
        # 评估模型
        accuracy = accuracy_score(y_test, y_pred)
        report = classification_report(y_test, y_pred, target_names=label_encoder.classes_)
        print(f"KNN 模型准确率: {accuracy:.4f}")
        print("KNN 分类报告:")
        print(report)
        dump(knn, 'knn.joblib')
        return knn

    def svc_utils(self, X_train, X_test, y_train, y_test, label_encoder):
        """
        SVC 分类
        """
        # 初始化 SVC 分类器
        svc = SVC(kernel='linear', random_state=24)
        # 训练模型
        svc.fit(X_train, y_train)
        # 预测测试集
        y_pred = svc.predict(X_test)
        # 评估模型
        accuracy = accuracy_score(y_test, y_pred)
        report = classification_report(y_test, y_pred, target_names=label_encoder.classes_)
        print(f"SVC 模型准确率: {accuracy:.4f}")
        print("SVC 分类报告:")
        print(report)
        dump(svc, 'svc.joblib')
        return svc

    def decision_tree_utils(self, X_train, X_test, y_train, y_test, label_encoder):
        """
        决策树分类
        """
        # 初始化决策树分类器
        dt = DecisionTreeClassifier(random_state=24, max_depth=10)
        # 训练模型
        dt.fit(X_train, y_train)
        # 预测测试集
        y_pred = dt.predict(X_test)
        # 评估模型
        accuracy = accuracy_score(y_test, y_pred)
        report = classification_report(y_test, y_pred, target_names=label_encoder.classes_)
        print(f"决策树模型准确率: {accuracy:.4f}")
        print("决策树分类报告:")
        print(report)
        dump(dt, 'decision_tree.joblib')
        return dt

    def random_forest_utils(self, X_train, X_test, y_train, y_test, label_encoder):
        """
        随机森林分类
        """
        # 初始化随机森林分类器
        rf = RandomForestClassifier(n_estimators=100, random_state=24, max_depth=10)
        # 训练模型
        rf.fit(X_train, y_train)
        # 预测测试集
        y_pred = rf.predict(X_test)
        # 评估模型
        accuracy = accuracy_score(y_test, y_pred)
        report = classification_report(y_test, y_pred, target_names=label_encoder.classes_)
        print(f"随机森林模型准确率: {accuracy:.4f}")
        print("随机森林分类报告:")
        print(report)
        dump(rf, 'random_forest.joblib')
        return rf

    def logistic_regression_utils(self, X_train, X_test, y_train, y_test, label_encoder):
        """
        逻辑回归分类
        """
        # 初始化逻辑回归分类器
        lr = LogisticRegression(random_state=24, max_iter=1000)
        # 训练模型
        lr.fit(X_train, y_train)
        # 预测测试集
        y_pred = lr.predict(X_test)
        # 评估模型
        accuracy = accuracy_score(y_test, y_pred)
        report = classification_report(y_test, y_pred, target_names=label_encoder.classes_)
        print(f"逻辑回归模型准确率: {accuracy:.4f}")
        print("逻辑回归分类报告:")
        print(report)
        dump(lr, 'logistic_regression.joblib')
        return lr

    def compare_all_models(self, X_train, X_test, y_train, y_test, label_encoder):
        """
        比较所有分类器的性能
        """
        models = {
            'KNN': self.knn_utils,
            'SVC': self.svc_utils,
            '决策树': self.decision_tree_utils,
            '随机森林': self.random_forest_utils,
            '逻辑回归': self.logistic_regression_utils
        }
        
        results = {}
        for name, model_func in models.items():
            print(f"\n{'='*50}")
            print(f"训练 {name} 模型中...")
            print(f"{'='*50}")
            model = model_func(X_train, X_test, y_train, y_test, label_encoder)
            y_pred = model.predict(X_test)
            accuracy = accuracy_score(y_test, y_pred)
            results[name] = accuracy
        
        print(f"\n{'='*50}")
        print("模型性能比较:")
        print(f"{'='*50}")
        for name, accuracy in sorted(results.items(), key=lambda x: x[1], reverse=True):
            print(f"{name}: {accuracy:.4f}")

if __name__ == '__main__':
    cu = ClassificationModel()
    X_train, X_test, y_train, y_test, label_encoder = cu.get_conditions()
    
    # 单独运行某个分类器
    # cu.knn_utils(X_train, X_test, y_train, y_test, label_encoder)
    # cu.svc_utils(X_train, X_test, y_train, y_test, label_encoder)
    # cu.decision_tree_utils(X_train, X_test, y_train, y_test, label_encoder)
    # cu.random_forest_utils(X_train, X_test, y_train, y_test, label_encoder)
    # cu.logistic_regression_utils(X_train, X_test, y_train, y_test, label_encoder)
    
    # 比较所有模型
    cu.compare_all_models(X_train, X_test, y_train, y_test, label_encoder)